<template>
  <BasicLayout>
    <template #wrapper>
      <el-card class="box-card">
        <!-- <el-form ref="queryForm" :model="queryParams" :inline="true">
                    <el-form-item label="适用地区" prop="province">
                        <el-select v-model="queryParams.province" remote filterable placeholder="请选择地区"
                            :remote-method="remoteMethod" :loading="selectload" style="width: 100%">
                            <el-option v-for="(item, index) in cityList" :key="index" :label="item.name"
                                :value="item.name" />
                        </el-select>
                    </el-form-item>
                    <el-form-item>
                        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
                        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
                    </el-form-item>
                </el-form> -->

        <el-table v-loading="loading" :data="courseList" border @selection-change="handleSelectionChange">
          <el-table-column type="selection" width="55" align="center" />
          <el-table-column label="编码" sortable="custom" width="75" type="index" />
          <el-table-column label="姓名" prop="name" :show-overflow-tooltip="true" />
          <el-table-column label="号码" prop="phone" :show-overflow-tooltip="true" />
          <el-table-column label="教学点" prop="province" :show-overflow-tooltip="true">
            <template slot-scope="scope">
              <span>{{ scope.row.area_point ? scope.row.area_point.name : '-' }}</span>
            </template>
          </el-table-column>
          <el-table-column label="就读院校" prop="province" :show-overflow-tooltip="true">
            <template slot-scope="scope">
              <span>{{ scope.row.college ? scope.row.college.colleges_name : '-' }}</span>
            </template>
          </el-table-column>
          <el-table-column label="操作" class-name="small-padding fixed-width" width="120">
            <template slot-scope="scope">
              <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)">
                移除学员
              </el-button>
            </template>
          </el-table-column>
        </el-table>
        <pagination
          v-show="total > 0"
          :total="total"
          :page.sync="queryParams.pageIndex"
          :limit.sync="queryParams.pageSize"
          @pagination="getList"
        />
      </el-card>
    </template>
  </BasicLayout>
</template>

<script>
import { getListByClassId, deleteStudent } from '@/api/admin/sys-grade'
import { getCity } from '@/api/admin/sys-course'
export default {
  name: 'SysAcademy',
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      chooseIds: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 总条数
      total: 0,
      // 表格数据
      courseList: [],
      // 查询参数
      queryParams: {
        pageIndex: 1,
        pageSize: 10,
        province: undefined
      },
      form: {},
      cityList: [],
      selectload: false
    }
  },

  created() {
    if (this.$route.params.id) {
      this.queryParams = {
        ...this.queryParams,
        class_id: this.$route.params.id
      }
    }
    this.getList()
    this.getCity()
  },
  methods: {
    getCity() {
      getCity().then(res => {
        this.cityList = res?.data || []
      })
    },
    /** 查询参数列表 */
    getList() {
      this.loading = true
      getListByClassId(this.queryParams).then(
        response => {
          this.courseList = response.data.list
          this.total = response.data.count
          this.loading = false
        }
      )
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageIndex = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
      this.getCity()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.chooseIds = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },

    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = (row.id && [row.id]) || this.chooseIds
      const courseId = this.$route.params && Number(this.$route.params.id)
      this.$confirm('是否确认移除吗?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(function() {
          return deleteStudent({
            id: ids,
            class_id: courseId
          })
        })
        .then(response => {
          if (response.code === 200) {
            this.msgSuccess(response.msg)
            this.getList()
          } else {
            this.msgError(response.msg)
          }
        })
        .catch(function() { })
    },
    // 表单重置
    reset() {
      this.form = {}
      this.resetForm('form')
    },
    remoteMethod(query) {
      if (query !== '') {
        this.selectload = true
        getCity({ name: query }).then(res => {
          this.cityList = res?.data || []
          this.selectload = false
        })
      } else {
        this.cityList = []
      }
    }
  }
}
</script>

<style>
.el-tag {
    margin-right: 5px;
}

.button-new-tag {
    height: 32px;
    line-height: 30px;
    padding-top: 0;
    padding-bottom: 0;
}

.input-new-tag {
    width: 90px;
    vertical-align: bottom;
}
</style>
